//
//  BmobAction.swift
//  Homepage
//
//  Created by kys-11 on 2019/3/13.
//  Copyright © 2019年 xl123. All rights reserved.
//

import UIKit

typealias Bmobsuccessljz  = (_ response : BmobObject) -> ()
typealias BmobSuccessljz = (_ bool:Bool) ->()
typealias BmobFailureljz = (_ error:Error) -> ()
typealias BmobUSuccessljz = (_ response : BmobUser) -> ()
typealias successljz = (_ response : [Any]) -> ()

class BmobAction: NSObject {

    
    
    func BmobLoning(nametext:String,password:String,success:@escaping BmobSuccessljz,failure:@escaping BmobFailureljz){
       
        let user = BmobUser()
        user.username = nametext
        user.password = password
        user.signUpInBackground { (isSuccessful, error) in
            if isSuccessful {
               success(true)
            }else{
                failure(error!)
            }
        }
        
    }
    

}

extension BmobAction{
    //登录方法
    class func Bmoregister(username:String,password:String,success:@escaping BmobUSuccessljz,failure:@escaping BmobFailureljz){
    
        BmobUser.loginInbackground(withAccount: username, andPassword: password) { (user, error) in
            
            if(user != nil){
                success((user!))
            }
            else{
                failure(error!)
            }
        }
}
    
   //定义一个用于上传动态的方法
    class func BomData(classname:String,username:String,content:String,imageArray:[String],userimage:String,success: @escaping Bmobsuccessljz,failure: @escaping BmobFailureljz){
        
        //创建一个BOm对像并设置表名
        let dynamic:BmobObject = BmobObject(className: classname)
        //添加数据
        dynamic.setObject(username, forKey: "username")
        dynamic.setObject(content, forKey: "content")
        dynamic.setObject(imageArray, forKey: "image")
        dynamic.setObject(userimage, forKey: "userimage")
        dynamic.saveInBackground { (isSussessful, error) in
            
            if (isSussessful){
                
                success(dynamic)
            }
            else {
                failure(error!)
            }
        }
        
    }
    
    //定义一个用于上传动态的方法
     class func BomDate(classname:String,content:String,imageArray:[String],success: @escaping Bmobsuccessljz,failure: @escaping BmobFailureljz){
         //创建一个BOm对像并设置表名
         let dynamic:BmobObject = BmobObject(className: classname)
         //添加数据
         dynamic.setObject(content, forKey: "content")
         dynamic.setObject(imageArray, forKey: "image")
         dynamic.saveInBackground { (isSussessful, error) in
             
             if (isSussessful){
                 
                 success(dynamic)
             }
             else {
                 failure(error!)
             }
         }
     }
    
    
    //查询多条数据
    class func Bomrequest(classname: String,success: @escaping successljz,failure: @escaping BmobFailureljz){
        
        let quert: BmobQuery = BmobQuery(className: classname)
        quert.order(byDescending: "updatedAt")//
        quert.limit = 3;//限制得到的结果条数为3条
        quert.findObjectsInBackground { (array, error) in
       
            if (array != nil){
                success(array!)
            }
            else{
                failure(error!)
            }
        }
            }
    //

}
